/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sn.controller;

import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import sn.metier.Personne;
import sn.service.PersonneService;

/**
 *
 * @author Harry
 */
@Controller
public class ModifProfilController {
    @Autowired
    private PersonneService personneService;

    @Transactional
    @RequestMapping(value="/modifier",method=RequestMethod.GET)
    public String modifierProfil(Model model,HttpSession session){
        if(session.isNew() || session.getAttribute("user") == null){
            return "indexNonConnecte";
        }
        Long iduser = (Long)session.getAttribute("user");
        Personne user = personneService.voirPersonne(iduser);
        model.addAttribute("user",user);
        return "modifProfil";

    }

    @RequestMapping(value="/modifier",method=RequestMethod.POST)
    public String modifierProfil(Model model,HttpSession session,String avatar, String pseudo,String email,String presentation, String passwd, String newpasswd, String newpasswdbis){
        if(session.isNew() || session.getAttribute("user") == null){
            return "indexNonConnecte";
        }
        Long iduser = (Long)session.getAttribute("user");
        Personne user = personneService.voirPersonne(iduser);

        model.addAttribute("user",user);
        
        try {
            if(pseudo == null || pseudo.length() == 0){
                model.addAttribute("error", "Le pseudo ne doit pas être vide");
                return "modifProfil";
            }
            if(email == null || email.length() == 0){
                model.addAttribute("error", "L'email ne doit pas être vide");
                return "modifProfil";
            }
            if(avatar == null || avatar.length() == 0){
                avatar = Personne.DEFAULT_AVATAR;
            }
            if((passwd == null || passwd.length() == 0) && (newpasswd == null || newpasswd.length() == 0) && (newpasswdbis == null || newpasswdbis.length() == 0)){

            }else if(passwd.length()>0 && newpasswd.length()>0 && newpasswdbis.length()>0){
                if(newpasswd.equals(newpasswdbis)){
                    personneService.modifierPasswd(iduser, passwd, newpasswd);
                    model.addAttribute("success","Password mis à jour");
                }else{
                    model.addAttribute("error", "Les 2 champs du nouveau mot de passe doivent être identiques");
                    return "modifProfil";
                }
            }else{
                model.addAttribute("error", "Si vous souhaitez modifier votre mot de passe les 3 champs de modifications doivent être remplis");
                return "modifProfil";
            }

            personneService.modifierProfil(iduser, pseudo, avatar, email);
        } catch (Exception e){
            model.addAttribute("error", e.getMessage());
            return "modifProfil";
        }
        
        Personne userfin = personneService.voirPersonne(iduser);

        model.addAttribute("user",userfin);
        return "modifProfil";
    }

}
